//
//  MBDGroupMainCell.swift
//  MasterBoy
//
//  Created by kyson on 2023/4/22.
//

import Foundation

import YYText
import SnapKit
import Kingfisher
import RxSwift
import RxCocoa

class MBDGroupMainCell: MBDCommonCell {

    private var titleLabel : YYLabel!
    private var subtitleLabel : YYLabel!
    private var uploaderAvatarImageView : UIImageView!
    private var uploaderLabel : YYLabel!
    private var groupCoverImageView : UIImageView!
    
    private var commontsButton : UIButton!
    private var praiseButton : UIButton!
    private var moreButton : UIButton!

    private var separaterLine : UIView!
    private var avatarDisposeBag : DisposeBag!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.setDynamicForgroundColor()
        
        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.initialize()
    }

    func initialize() {
        
        uploaderAvatarImageView = UIImageView()
        uploaderAvatarImageView = UIImageView()
        uploaderAvatarImageView.isUserInteractionEnabled = true
        uploaderAvatarImageView.backgroundColor = UIColor(colorHex: 0x4273d0)
        uploaderAvatarImageView.layer.cornerRadius = MBDMainCellModel.uploaderAvatarHeight / 2
        uploaderAvatarImageView.clipsToBounds = true
        self.contentView.addSubview(uploaderAvatarImageView)
        
        avatarDisposeBag = DisposeBag()
        let avatarTapGesture = UITapGestureRecognizer()
        uploaderAvatarImageView.addGestureRecognizer(avatarTapGesture)
        uploaderAvatarImageView.image = UIImage(named: "item_thumb")
        avatarTapGesture.rx.event.bind(onNext: { recognizer in
            //回调
            let realCellModel = self.cellModel as! MBDMainCellModel
            if let avatarClicedBlock = realCellModel.posterAvatarClicedBlock {
                avatarClicedBlock()
            }
            
        }).disposed(by: avatarDisposeBag)
        
        uploaderLabel = YYLabel()
        uploaderLabel.text = "无名氏用户"
        uploaderLabel.isUserInteractionEnabled = true
        
        let uploaderTapAction : YYTextAction = {_,_,_,_ in
            //回调
           
            let realCellModel = self.cellModel as! MBDMainCellModel
            if let avatarClicedBlock = realCellModel.posterAvatarClicedBlock {
                avatarClicedBlock()
            }
        }
        
        uploaderLabel.textTapAction = uploaderTapAction
        
        self.contentView.addSubview(uploaderLabel)
        
        titleLabel = YYLabel()
        self.contentView.addSubview(titleLabel)
        
        subtitleLabel = YYLabel()
        self.contentView.addSubview(subtitleLabel)
        
        separaterLine = UIView()
        self.contentView.addSubview(separaterLine)
        separaterLine.setDynamicSeperateLineColor()
        
        groupCoverImageView = UIImageView()
        groupCoverImageView.layer.cornerRadius = 4
        groupCoverImageView.clipsToBounds = true
        self.contentView.addSubview(groupCoverImageView)
        
        commontsButton = UIButton()
        commontsButton.setTitleColor(.gray, for: .normal)
        commontsButton.titleLabel?.font = .systemFont(ofSize: 14)
        commontsButton.setImage(UIImage.init(named: "ico_gdetail_comments"), for: .normal)
        commontsButton.setTitle(" ", for: .normal)
        self.contentView.addSubview(commontsButton)
        
        praiseButton = UIButton()
        praiseButton.setTitleColor(.gray, for: .normal)
        praiseButton.titleLabel?.font = .systemFont(ofSize: 14)
        praiseButton.setImage(UIImage.init(named: "ico_gdetail_praise"), for: .normal)
        praiseButton.setTitle(" ", for: .normal)
        self.contentView.addSubview(praiseButton)
        
        moreButton = UIButton()
        moreButton.setTitleColor(.gray, for: .normal)
        moreButton.titleLabel?.font = .systemFont(ofSize: 14)
        moreButton.setImage(UIImage.init(named: "ico_gdetail_more"), for: .normal)
        moreButton.setTitle(" more", for: .normal)
        self.contentView.addSubview(moreButton)
        moreButton.isHidden = true
        
        uploaderAvatarImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(MBDMainCellModel.uploaderAvatarHeight, MBDMainCellModel.uploaderAvatarHeight))
            make.top.equalTo(MBDMainCellModel.topMargin)
            make.left.equalTo(MBDMainCellModel.leftMargin)
        }
                
        uploaderLabel.snp.makeConstraints { make in
            make.left.equalTo(uploaderAvatarImageView.snp.right).offset(8)
            make.centerY.equalTo(uploaderAvatarImageView)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(18)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(uploaderAvatarImageView)
            make.top.equalTo(uploaderAvatarImageView.snp.bottom).offset(MBDMainCellModel.marginBetweenAvatarAndTitle)
            make.right.equalTo(self.contentView).offset(-15)
            make.height.equalTo(0)
        }
        
        subtitleLabel.snp.makeConstraints { make in
            make.left.equalTo(MBDMainCellModel.marginSubtitleLeft)
            make.top.equalTo(titleLabel.snp.bottom).offset(MBDMainCellModel.marginBetweenAvatarAndTitle)
            make.right.equalTo(-MBDMainCellModel.marginSubtitleRight)
            make.height.equalTo(0)
        }
        
        groupCoverImageView.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(subtitleLabel.snp.bottom).offset(MBDMainCellModel.groupCoverMarginTop)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(0)
        }
        
        commontsButton.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(groupCoverImageView.snp.bottom).offset(MBDMainCellModel.groupCoverMarginBottom)
            make.width.equalTo(50)
            make.height.equalTo(MBDMainCellModel.praiseButtonHeight)
        }
        
        praiseButton.snp.makeConstraints { make in
            make.left.equalTo(UIScreen.main.bounds.width / 2)
            make.top.equalTo(commontsButton)
            make.height.equalTo(MBDMainCellModel.praiseButtonHeight)
        }
        
        moreButton.snp.makeConstraints { make in
            make.right.equalTo(titleLabel)
            make.top.equalTo(commontsButton)
            make.height.equalTo(MBDMainCellModel.praiseButtonHeight)
        }
        
        let scale = UIScreen.main.scale
        let h = ceil(0.5 * scale) / scale
        
        separaterLine.snp.makeConstraints { make in
            make.left.equalTo(groupCoverImageView)
            make.right.equalToSuperview()
            make.height.equalTo( h )
            make.bottom.equalToSuperview()
        }
    }
    
    deinit {
            print("MBDGroupMainCell was deallocated")
    }
    
    override var cellModel: Any! {
        didSet {
            let realCellModel = cellModel as! MBDMainCellModel
            
            self.titleLabel.textLayout = realCellModel.titlelayout
            titleLabel.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.titlelayout!.textBoundingSize.height)
            }
            
            self.subtitleLabel.textLayout = realCellModel.subtitlelayout
            subtitleLabel.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.subtitlelayout!.textBoundingSize.height)
            }
            
            self.uploaderLabel.textLayout = realCellModel.uploaderlayout
            
            if let coverURL = realCellModel.groupCoverURL {
                
                let url = URL(string: coverURL)
                
                self.groupCoverImageView.kf.setImage(with: url)
                
                groupCoverImageView.snp.updateConstraints { make in
                    make.height.equalTo(MBDMainCellModel.groupCoverHeight)
                }
            } else {
                debugPrint("----")
            }
            
            if let avatarURL = realCellModel.avatarURL {
                
                let url = URL(string: avatarURL)
                self.uploaderAvatarImageView.kf.setImage(with: url)                
                
            } else {
                debugPrint("----")
            }
            
            self.commontsButton.setTitle(" ".appending(realCellModel.commentsCount), for: .normal)
            self.praiseButton.setTitle(" ".appending(realCellModel.praiseCount), for: .normal)

        }
    }

}
